﻿using Api.BLL;
using Api.Model;
using Api.Web.filter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Api.Web.helper;


namespace Api.Web.Controllers
{
    public class MethodController : BaseController
    {
        public ActionResult Detail(int projectId, int categoryId, int methodId)
        {         
            //獲取方法詳情
            var r = (from m in Db.Current.Methods.Where(s => s.UserId == userId && s.ProjectId == projectId)
                     join p in Db.Current.Parameters.Where(s => s.UserId == userId && s.ProjectId == projectId) on m.Id equals p.MethodId into g
                     where m.Id == methodId
                     select new { m, g }).FirstOrDefault();


            if (r == null || r.m == null || r.g == null) return View("_404");
            r.m.Parameters = r.g.ToList();

            ViewBag.methodId = methodId;
            ViewBag.categoryId = categoryId;
            return View(r.m);
        }

        public JsonResult GetLeft(int categoryId = 0, int projectId = 0)
        {
            if (categoryId > 0 && projectId > 0)
            {

                //這裡為什麼會報錯呢,其實linq1.不容易封裝方法,或則說,你忍不住想要多查一些東西時,
                var Categorys = from c in Db.Current.Categories.Where(s => s.Id != categoryId && s.ProjectId == projectId)
                                join m in Db.Current.Methods.Where(b => b.ProjectId == projectId) on c.ProjectId equals m.ProjectId into g
                                select new
                                {
                                    c.Id,
                                    c.Name,
                                    method = g.Select(b => new { b.Id, b.CategoryId }).FirstOrDefault(b => b.CategoryId == c.Id)
                                };



                //看來過於複雜,難以處理啊
                var categoryMethods = (from c in Db.Current.Categories.Where(s => s.UserId == userId && s.ProjectId == projectId)
                                       join m in Db.Current.Methods.Where(s => s.UserId == userId && s.ProjectId == projectId) on c.Id equals m.CategoryId into g
                                       join c2 in Db.Current.Categories.Where(s => s.UserId == userId && s.ProjectId == projectId) on c.ProjectId equals c2.ProjectId into clist
                                       join m2 in Db.Current.Methods.Where(s => s.UserId == userId && s.ProjectId == projectId) on c.ProjectId equals m2.ProjectId into glist
                                       where c.Id == categoryId
                                       select new
                                       {
                                           c.Id,
                                           c.Name,
                                           Methods = g.Select(s => new
                                           {
                                               CategoryId = c.Id,
                                               s.Name,
                                               s.Url,
                                               s.Id
                                           }),
                                           //Categorys = clist.Where(s => s.Id != c.Id).Select(s => new
                                           //{
                                           //    s.Id,
                                           //    s.Name,
                                           //    method = glist.Select(k => new { k.Id, k.CategoryId }).FirstOrDefault(k => k.CategoryId == s.Id)
                                           //})
                                       }).FirstOrDefault();
                return Json(categoryMethods, JsonRequestBehavior.AllowGet);
            }
            return Json(null, JsonRequestBehavior.AllowGet);
        }
    }
}